package com.hkccommunity.service.impl;
import com.hkccommunity.entity.User;
import com.hkccommunity.mapper.UserMapper;
import com.hkccommunity.service.UserService;
import com.hkccommunity.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


public class UserServiceImpl implements UserService {

    private SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();


    @Override
    public User getUserByPhone(String phone) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserByPhone(phone);
        sqlSession.close();
        return user;
    }

    /**
     * 根据id获取
     * @param id 用户id
     * @return
     */
    @Override
    public User getUserById(Long id) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(id);
        sqlSession.close();
        return user;
    }

    @Override
    public int insertUser(User user) {
        SqlSession sqlSession = factory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.insertUser(user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
}
